
#ifndef PLAYER_H_
#define PLAYER_H_

#include "World.h"

float posX, posY, posZ;
const float	speed = 0.2f;
int jumping;

const float boundsX = -0.25f, boundsY = -1.5f, boundsZ = -0.25f;
const float boundsW = (0.5f), boundsH = (1.5f), boundsD = (0.5f);

void setPosition(float x, float y, float z) {
	posX = x;
	posY = y;
	posZ = z;
}

void jump() {
	if(jumping == 0) jumping++;
}

bool collision(float x, float y, float z) {
	float xp = (posX+x+boundsX+(SHOW_X/2));
	float yp = (posY+y+boundsY+(SHOW_Y/2));
	float zp = (posZ+z+boundsZ+(SHOW_Z/2));
	for(int i = (int) (-1+boundsX); i < 2+boundsW; i++) {
	   for(int j = (int) (-1+boundsY); j < 2+boundsH; j++) {
	      for(int k = (int) (-1+boundsZ); k < 2+boundsD; k++) {
	         int xt = (int) (xp + i);
	         int yt = (int) (yp + j);
	         int zt = (int) (zp + k);
	         if(getBlockId(xt, yt, zt) != 0) {
	        	 if(((posX + x + boundsX + boundsW + (SHOW_X/2)) > (xt)) &&
	        		((posY + y + boundsY + boundsH + (SHOW_Y/2)) > (yt)) &&
	        		((posZ + z + boundsZ + boundsD + (SHOW_Z/2)) > (zt)) &&
	        		((posX + x + boundsX + (SHOW_X/2)) < (xt + (1))) &&
	        		((posY + y + boundsY + (SHOW_Y/2)) < (yt + (1))) &&
	        		((posZ + z + boundsZ + (SHOW_Z/2)) < (zt + (1)))) {
	        		 return true;
	        	 }
	         }
	      }
	   }
	}
	return false;
}

bool move(float &x, float &y, float &z) {
	y -= 0.0981f;
	if(jumping > 0 && jumping < 8) {
		y = speed;
		jumping++;
	}
	bool flag = (x != 0.0f) || (y != 0.0f) || (z != 0.0f);
	if(collision(x, y, z)) {
		if(collision(x, 0.0f, 0.0f)) x = 0.0f;
		if(collision(0.0f, y, 0.0f)) y = 0.0f;
		if(collision(0.0f, 0.0f, z)) z = 0.0f;
		flag = false;
	}
	if(y == 0) jumping = y;
	posX += x;
	posY += y;
	posZ += z;
	x /= 2;
	y /= 2;
	z /= 2;
	return flag;
}

#endif /* PLAYER_H_ */
